<template>
  <div class="app-container">
    <el-row>
      <el-col :span="12">
        <div class="grid-content bg-purple">
          <el-button type="success" @click="addDataBtn">新增</el-button>
          <el-button type="warning" @click="moreDel">多项删除</el-button>
        </div>
      </el-col>
      <el-col :span="12">
        <div class="grid-content bg-purple-light">
          <el-input
            v-model="iptSearch"
            placeholder="请输入内容"
            class="input-with-select"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="onSearch"
            ></el-button>
          </el-input>
        </div>
      </el-col>
    </el-row>

    <el-table
      style="width: 100%; margin: 10px auto"
      @selection-change="handleSelectionChange"
      @sort-change="onSortChange"
      height="350"
      :data="list"
    >
      <el-table-column fixed type="selection" width="50"></el-table-column>
      <el-table-column
        fixed
        type="index"
        label="#"
        width="50"
      ></el-table-column>
      <el-table-column
        fixed
        prop="title"
        label="标题"
        width="300"
      ></el-table-column>
      <el-table-column prop="type" label="类型" width="200"></el-table-column>
      <el-table-column prop="author" label="作者" width="200"></el-table-column>
      <el-table-column
        prop="time"
        label="时间"
        sortable
        width="150"
      ></el-table-column>
      <el-table-column
        prop="date"
        label="日期"
        sortable
        width="200"
      ></el-table-column>
      <el-table-column fixed="right" label="操作" width="300">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="onlook(scope.row)">查看</el-button>
          <el-button type="text" size="small" @click="delDataClk(scope.row.id)"
            >删除</el-button
          >
          <el-button type="text" size="small" @click="upDataClk(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 抽屉 -->
    <el-drawer
      :title="title"
      :visible.sync="drawer"
      :before-close="handleClose"
      :with-header="true"
    >
      <el-form
        :model="form"
        size="mini"
        style="padding: 0 5%"
        ref="form"
        label-width="100px"
        class="demo-form"
      >
        <el-form-item label="标题" prop="name">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="类别" prop="resource">
          <el-select v-model="form.cid" placeholder="请选择类别">
            <el-option
              v-for="(item, index) in classifyList"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="作者" prop="name">
          <el-input :disabled="true" v-model="form.author"></el-input>
        </el-form-item>
        <el-form-item label="用时" prop="desc">
          <el-select v-model="form.time" placeholder="请选择用时">
            <el-option label="一分钟" value="1"></el-option>
            <el-option label="二分钟" value="2"></el-option>
            <el-option label="三分钟" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容">
          <mavonEditor
            style="max-height: 300px; overflow-y: scroll"
            v-model="form.content"
          ></mavonEditor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addClk(form)">{{
            btnText
          }}</el-button>
          <el-button @click="cancelClk">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import { mavonEditor } from "mavon-editor";
import "mavon-editor/dist/css/index.css";
import { mapState, mapActions, mapMutations } from "vuex";
export default {
  data() {
    return {
      tableData: [],
      drawer: false,
      form: {
        author: "situ",
      },
      title: "新增",
      btnText: "立即创建",
      isShow: false,
      arrDel: [],
      idAs: "",
      iptSearch: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    ...mapActions("msbd/msbd", ["getList", "addList", "delList", "updateList"]),
    ...mapMutations("msbd/msbd", ["CURRENT_PAGE", "PAGE_SIZE","SEARCH"]),
    // 搜索
    onSearch() {
      this.SEARCH(this.iptSearch)
      this.getList()
    },
    // form 提交
    async addClk() {
      if (this.form.id) {
        this.updateList(this.form);
        this.getList();
      } else {
        await this.addList(this.form);
        this.getList();
      }
      this.drawer = false;
      this.form = {
        author: "situ",
      };
    },
    //form 取消
    cancelClk() {
      this.drawer = false;
    },
    // 删除
    delDataClk(value) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delList(value);
          this.getList();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    moreDel() {
      //多删除
      this.delDataClk(this.sid)
    },
    upDataClk(row) {
      this.drawer = true;
      this.form = { ...row };
    },
    addDataBtn() {
      //增加
      this.drawer = true;
    },
    handleSelectionChange(sid) {
      //多选
      let arr=[];
      for (var i=0;i<sid.length;i++) {
        arr.push(sid[i].id)
      }
      this.sid = arr.join(',')
      console.log(arr);
    },
    //排序
    onSortChange(value) {},
    //抽屉关闭
    handleClose(done) {
      this.drawer = false;
    },
    // 每页几条
    async handleSizeChange(val) {
      this.PAGE_SIZE(val);
      this.getList();
    },
    //当前页
    async handleCurrentChange(val) {
      this.CURRENT_PAGE(val);
      this.getList();
    },
    //查看
    onlook(row){
      this.drawer=true
      this.form = {...row}
    }
  },
  computed: {
    ...mapState("msbd/msbd", [
      "list",
      "currentPage",
      "pageSize",
      "total",
      "classifyList",
    ]),
  },
  components: {
    mavonEditor,
  },
};
</script>

<style scoped>
</style>